package com.ny.controller;

import com.ny.entity.Classrooms;
import com.ny.entity.Reservations;
import com.ny.entity.Users;
import com.ny.service.ClassroomService;
import com.ny.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.awt.print.PrinterAbortException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
@Controller
public class ApplyController {
    @Autowired
    private ClassroomService classroomService;
    @Autowired
    private ReservationService reservationService;
    @RequestMapping("/apply")
    public  String apply(@RequestParam int classroomId , int userId , Model model){
        model.addAttribute("classroomId",classroomId);
        model.addAttribute("userId",userId);
        return "apply";
    }

    @RequestMapping("submitApply")
    public  String submitApply(@RequestParam int classroomId,
                               @RequestParam int userId,
                               @RequestParam String startTime,
                               @RequestParam String endTime,
                               @RequestParam String reason)throws ParseException{

        classroomService.updateClassroomStatusById(classroomId,"申请中");

        Reservations reservations = new Reservations();
        reservations.setStatus("申请中");
        reservations.setReason(reason);
        Classrooms classrooms = new Classrooms();
        classrooms.setClassroomId(classroomId);
        reservations.setClassroom(classrooms);
        Users users = new Users();
        users.setUserId(userId);
        reservations.setUser(users);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
        reservations.setStartDatetime(sdf.parse(startTime));
        reservations.setEndDatetime(sdf.parse(endTime));
        // 添加预约记录
        reservationService.addReservation(reservations);
        return "redirect:/classroomList";



    }
}
